package org.example;

import org.example.entity.CustomerInfo;
import org.example.entity.CustomerOrder;
import org.example.entity.CustomerRepayPlan;
import org.example.generator.DataGenerator;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 测试逾期逻辑修复
 */
public class TestOverdueLogicFix {
    
    public static void main(String[] args) {
        System.out.println("=== 测试逾期逻辑修复 ===");
        
        // 生成小量数据进行测试
        DataGenerator.DataGenerationResult result = DataGenerator.generateCompleteDataSet();
        
        List<CustomerOrder> orders = result.getOrders();
        List<CustomerRepayPlan> repayPlans = result.getRepayPlans();
        
        System.out.println("生成订单数量: " + orders.size());
        System.out.println("生成还款计划数量: " + repayPlans.size());
        
        // 验证逾期逻辑
        validateOverdueLogic(orders, repayPlans);
        
        // 分析还款计划状态分布
        Map<String, Long> repayStatusDistribution = repayPlans.stream()
                .collect(Collectors.groupingBy(CustomerRepayPlan::getRepayStatus, Collectors.counting()));
        System.out.println("还款计划状态分布: " + repayStatusDistribution);
        
        System.out.println("=== 测试完成 ===");
    }
    
    /**
     * 验证逾期逻辑
     */
    private static void validateOverdueLogic(List<CustomerOrder> orders, List<CustomerRepayPlan> repayPlans) {
        System.out.println("\n=== 验证逾期逻辑 ===");
        
        // 按订单分组还款计划
        Map<String, List<CustomerRepayPlan>> orderPlans = repayPlans.stream()
                .collect(Collectors.groupingBy(CustomerRepayPlan::getOrderNo));
        
        int invalidOrders = 0;
        int totalOrdersWithOverdue = 0;
        
        for (CustomerOrder order : orders) {
            List<CustomerRepayPlan> plans = orderPlans.get(order.getOrderNo());
            if (plans != null) {
                // 按期数排序
                plans.sort((p1, p2) -> p1.getBillTenor().compareTo(p2.getBillTenor()));
                
                boolean hasOverdue = false;
                boolean hasSettledAfterOverdue = false;
                
                for (CustomerRepayPlan plan : plans) {
                    if ("逾期".equals(plan.getRepayStatus())) {
                        hasOverdue = true;
                        totalOrdersWithOverdue++;
                    }
                    if (hasOverdue && "已结清".equals(plan.getRepayStatus())) {
                        hasSettledAfterOverdue = true;
                        invalidOrders++;
                        System.out.println("❌ 订单 " + order.getOrderNo() + " 期数 " + plan.getBillTenor() + " 存在逾期后结清的情况！");
                    }
                }
            }
        }
        
        System.out.println("有逾期的订单数量: " + totalOrdersWithOverdue);
        if (invalidOrders == 0) {
            System.out.println("✅ 逾期逻辑验证通过：没有逾期后结清的情况");
        } else {
            System.out.println("❌ 发现 " + invalidOrders + " 个订单存在逾期后结清的情况，bug未完全修复！");
        }
    }
}




